
cd('.')
addpath('matlab functions')

OUT = struct();

for sbj = {'math','reading'}
    TBL = cell2table(cell([17 1]),'VariableNames',{'names'});
    for m = 1:17
        vamdl = load(sprintf('output/model selection/vam_%s_matchvars%d.mat',sbj{1},m));

        if ~isequal(TBL.names(1:(m-1))',vamdl.tchcoef(1:(m-1)))
            error('variables not sequential')
        end

        TBL.names(m) = vamdl.tchcoef(m);
        TBL.NumRandomCoefficients(m) = vamdl.NumRandomCoefficients;
        TBL.LogLikelihood(m) = vamdl.LogLikelihood;
        TBL.AIC(m) = vamdl.AIC;
        TBL.adjR2(m) = vamdl.RsquaredAdjusted;
        TBL.pval(m) = nan;
        TBL.MSE(m) = vamdl.cv_MSE;
    end

    LL_base = TBL.LogLikelihood(1);
    AIC_base = TBL.AIC(1);

    LR = 2*diff(TBL.LogLikelihood);
    nr = TBL.NumRandomCoefficients(2:end);
    TBL.pval(2:end) = 1 - (chi2cdf(LR,nr) + chi2cdf(LR,nr-1))/2;
    TBL.LogLikelihood = TBL.LogLikelihood - LL_base;
    TBL.AIC = TBL.AIC - AIC_base;
    TBL.LogLikelihood = [];

    TBL.names = regexprep(TBL.names,'lag_(math|reading)score_sq','\$A_\{t-1\}^2\$');
    TBL.names = regexprep(TBL.names,'lag_(math|reading)score','\$A_\{t-1\}\$');
    TBL.names = regexprep(TBL.names,'poverty','FRL');
    TBL.names = regexprep(TBL.names,'female','FEM');
    TBL.names = regexprep(TBL.names,'race_black','BL');
    TBL.names = regexprep(TBL.names,'lep','LEP');
    TBL.names = regexprep(TBL.names,'^([^X]+)X(.+)','($1 \$\\times\$ $2)');
    TBL.names = replace(TBL.names,'intercept','Intercept Only');
    TBL.names(2:end) = strcat('+',{' '},TBL.names(2:end));
    TBL.names = strcat('\scriptsize{',TBL.names,'}');

    OUT.(sbj{1}).variables = TBL;

    TBL = cell2table(cell([3 1]),'VariableNames',{'names'});
    for m = 1:3
        vamdl = load(sprintf('output/model selection/vam_%s_numcomp%d.mat',sbj{1},m));

        TBL.names(m) = {num2str(m)};
        TBL.NumRandomCoefficients(m) = vamdl.NumRandomCoefficients;
        TBL.LogLikelihood(m) = vamdl.LogLikelihood - LL_base;
        TBL.AIC(m) = vamdl.AIC - AIC_base;
        TBL.adjR2(m) = vamdl.RsquaredAdjusted;
        TBL.pval(m) = nan;
        TBL.MSE(m) = vamdl.cv_MSE;
    end
    TBL.LogLikelihood = [];
    TBL.adjR2(2:end) = nan;
    TBL.names = replace(TBL.names,'1','MVN');
    TBL.names = replace(TBL.names,'2','2 Comp. GM');
    TBL.names = replace(TBL.names,'3','3 Comp. GM ');
    TBL.names = strcat('\scriptsize{',TBL.names,'}');

    OUT.(sbj{1}).component = TBL;

end

TBL = [table2cell(OUT.math.variables(:,:)) table2cell(OUT.reading.variables(:,3:end))];
latextbl(TBL,'tables and figures/tables/model_selection_variable.tex', ...
        {'%s','%d','%0.0f','%0.4f','%0.4f','%0.4f', ...
        '%0.0f','%0.4f','%0.4f','%0.4f'});

TBL = [table2cell(OUT.math.component(:,:)) table2cell(OUT.reading.component(:,3:end))];
latextbl(TBL,'tables and figures/tables/model_selection_component.tex', ...
        {'%s','%d','%0.0f','%0.4f','%0.4f','%0.4f', ...
        '%0.0f','%0.4f','%0.4f','%0.4f'});


